package com.szt.mobile.inventory.app;

import android.app.Application;
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.ServiceConnection;
import android.net.ConnectivityManager;
import android.net.wifi.WifiManager;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;

import com.handset.Device;
import com.isoftstone.mis.ffair.comp.network.FFHttpClientComponent;
import com.isoftstone.mis.ffair.util.log.FFLog;
import com.szt.mobile.inventory.R;
import com.szt.mobile.inventory.Service.DeviceService;
import com.szt.mobile.inventory.common.constants.IntfConstants;
import com.szt.mobile.inventory.common.utils.CrashHandler;
import com.szt.mobile.inventory.common.utils.SpUtil;
import com.szt.mobile.inventory.reciver.NetBroadCastReciver;

import org.xutils.x;

public class HandsetApplication extends Application {
    private static HandsetApplication app;
    private Intent intent;
    private DeviceService mService;
    public Device device;
    boolean bBind = false;
    CrashHandler mCrashHandler;

    public static HandsetApplication getInstance(){
        return app;
    }
    public ServiceConnection conn = new ServiceConnection() {

        @Override
        public void onServiceDisconnected(ComponentName name) {
            System.out.println("----Service Disconnected----");
            bBind = false;
        }

        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            System.out.println("----Service Connected!----");
            DeviceService.MyBinder binder = (DeviceService.MyBinder) service;
            mService = binder.GetService();
            mService.SetBinder(getApplicationContext(), null);
            device = mService.device;
            bBind = true;
        }
    };

    @Override
    public void onCreate() {
        super.onCreate();
        bindService();
        app=this;
        x.Ext.init(this);
        FFLog.setDebug(false);
        FFLog.setLevel(Log.ERROR);
        FFLog.setFilePath("ssscc/log");
        mCrashHandler = CrashHandler.getInstance();
//        mCrashHandler.init(this);
        FFHttpClientComponent.enableDebugLogging(true);

        // 必须先配置了ip和端口才能登陆
        IntfConstants.IP = SpUtil.getInstance(this).getKeyStringValue(SpUtil.Key.KEY_IP, "");
        IntfConstants.PORT = SpUtil.getInstance(this).getKeyStringValue(SpUtil.Key.KEY_PORT, "80");

        IntfConstants.setBaseUrl();

        setBreoadcast();
    }

    private void setBreoadcast() {
        BroadcastReceiver receiver=new NetBroadCastReciver();
        IntentFilter filter=new IntentFilter();
        filter.addAction(WifiManager.WIFI_STATE_CHANGED_ACTION);
        filter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION);
        filter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
        registerReceiver(receiver, filter);
    }

    public void bindService() {
        intent = new Intent(this, DeviceService.class);
        startService(intent);
        try {
            bindService(intent, conn, Service.BIND_AUTO_CREATE);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void unBindService() {
        unbindService(conn);
        stopService(intent);
    }

    public boolean checkServiceAndDevice() {
        if (bBind) {
            if (device.getDeviceIsOpen()) {
                return true;
            } else {
                Toast.makeText(this, R.string.msg_device_not_open,
                        Toast.LENGTH_SHORT).show();
            }
        } else {
            Toast.makeText(this, R.string.msg_service_init_fail,
                    Toast.LENGTH_SHORT).show();
        }
        return false;
    }
}
